/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCGridRow.h"

namespace FaceCat{
    FCGridRowStyle::FCGridRowStyle(){
        m_backColor = FCColor_None;
        m_font = new FCFont(L"Default", 14, false, false, false);;
        m_textColor = FCColor_Text;
        m_hoveredBackColor = FCColor_Hovered;
        m_hoveredTextColor = FCColor_Text;
        m_selectedBackColor = FCColor_Pushed;
        m_selectedTextColor = FCColor_Text;
    }
    
    FCGridRowStyle::~FCGridRowStyle(){
        if(m_font){
            delete m_font;
            m_font = 0;
        }
    }
    
    Long FCGridRowStyle::getBackColor(){
        return m_backColor;
    }
    
    void FCGridRowStyle::setBackColor(Long value){
        m_backColor = value;
    }
    
    FCFont* FCGridRowStyle::getFont(){
        return m_font;
    }
    
    void FCGridRowStyle::setFont(FCFont *value){
        if(value){
            if(!m_font){
                m_font = new FCFont();
            }
            m_font->copy(value);
        }
        else{
            if(m_font){
                delete m_font;
                m_font = 0;
            }
        }
    }
    
    Long FCGridRowStyle::getHoveredBackColor(){
        return m_hoveredBackColor;
    }
    
    void FCGridRowStyle::setHoveredBackColor(Long value){
        m_hoveredBackColor = value;
    }
    
    Long FCGridRowStyle::getHoveredTextColor(){
        return m_hoveredTextColor;
    }
    
    void FCGridRowStyle::setHoveredTextColor(Long value){
        m_hoveredTextColor = value;
    }
    
    Long FCGridRowStyle::getSelectedBackColor(){
        return m_selectedBackColor;
    }
    
    void FCGridRowStyle::setSelectedBackColor(Long value){
        m_selectedBackColor = value;
    }
    
    Long FCGridRowStyle::getSelectedTextColor(){
        return m_selectedTextColor;
    }
    
    void FCGridRowStyle::setSelectedTextColor(Long value){
        m_selectedTextColor = value;
    }
    
    Long FCGridRowStyle::getTextColor(){
        return m_textColor;
    }
    
    void FCGridRowStyle::setTextColor(Long value){
        m_textColor = value;
    }
    
    void FCGridRowStyle::copy(FCGridRowStyle *style){
        setBackColor(style->getBackColor());
        setFont(style->getFont());
        setTextColor(style->getTextColor());
        setHoveredBackColor(style->getHoveredBackColor());
        setHoveredTextColor(style->getHoveredTextColor());
        setSelectedBackColor(style->getSelectedBackColor());
        setSelectedTextColor(style->getSelectedTextColor());
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    FCGridRow::FCGridRow(){
        m_allowEdit = false;
        m_bounds.left = 0;
        m_bounds.top = 0;
        m_bounds.right = 0;
        m_bounds.bottom = 0;
        m_editButton = 0;
        m_editState = 0;
        m_grid = 0;
        m_height = 20;
        m_horizontalOffset = 0;
        m_index = -1;
        m_tag = 0;
        m_visible = true;
        m_visibleIndex = -1;
    }
    
    FCGridRow::~FCGridRow(){
        for(int i = 0; i < m_cells.size(); i++){
            delete m_cells.get(i);
        }
        m_cells.clear();
        m_editButton = 0;
        m_grid = 0;
        m_tag = 0;
    }
    
    bool FCGridRow::allowEdit(){
        return m_allowEdit;
    }
    
    void FCGridRow::setAllowEdit(bool value){
        m_allowEdit = value;
    }
    
    FCRect FCGridRow::getBounds(){
        return m_bounds;
    }
    
    void FCGridRow::setBounds(FCRect value){
        m_bounds = value;
    }
    
    FCView* FCGridRow::getEditButton(){
        return m_editButton;
    }
    
    void FCGridRow::setEditButton(FCView *value){
        m_editButton = value;
    }
    
    FCGrid* FCGridRow::getGrid(){
        return m_grid;
    }
    
    void FCGridRow::setGrid(FCGrid *value){
        m_grid = value;
    }
    
    int FCGridRow::getHeight(){
        return m_height;
    }
    
    void FCGridRow::setHeight(int value){
        m_height = value;
    }
    
    int FCGridRow::getHorizontalOffset(){
        return m_horizontalOffset;
    }
    
    void FCGridRow::setHorizontalOffset(int value){
        m_horizontalOffset = value;
    }
    
    int FCGridRow::getIndex(){
        return m_index;
    }
    
    void FCGridRow::setIndex(int value){
        m_index = value;
    }
    
    Object FCGridRow::getTag(){
        return m_tag;
    }
    
    void FCGridRow::setTag(Object value){
        m_tag = value;
    }
    
    bool FCGridRow::isVisible(){
        return m_visible;
    }
    
    void FCGridRow::setVisible(bool value){
        m_visible = value;
    }
    
    int FCGridRow::getVisibleIndex(){
        return m_visibleIndex;
    }
    
    void FCGridRow::setVisibleIndex(int value){
        m_visibleIndex = value;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void FCGridRow::addCell(FCGridColumn *column, FCGridCell *cell){
        cell->setGrid(m_grid);
        cell->setColumn(column);
        cell->setRow(this);
        m_cells.add(cell);
        cell->onAdd();
    }
    
    void FCGridRow::addCell(const String& columnName, FCGridCell *cell){
        cell->setGrid(m_grid);
        cell->setColumn(m_grid->getColumn(columnName));
        cell->setRow(this);
        m_cells.add(cell);
        cell->onAdd();
    }
    
    
    void FCGridRow::addCell(int columnIndex, FCGridCell *cell){
        cell->setGrid(m_grid);
        cell->setColumn(m_grid->getColumn(columnIndex));
        cell->setRow(this);
        m_cells.add(cell);
        cell->onAdd();
    }
    
    void FCGridRow::clearCells(){
        for(int i = 0; i < m_cells.size(); i++){
            m_cells.get(i)->onRemove();
            delete m_cells.get(i);
        }
        m_cells.clear();
    }
    
    FCGridCell* FCGridRow::getCell(FCGridColumn *column){
        for(int i = 0; i < m_cells.size(); i++){
            FCGridCell *cell = m_cells.get(i);
            if(cell->getColumn() == column){
                return cell;
            }
        }
        return 0;
    }
    
    FCGridCell* FCGridRow::getCell(int columnIndex){
        int cellsSize = (int)m_cells.size();
        if(cellsSize > 0){
            if(columnIndex >= 0 && columnIndex < cellsSize){
                if(m_cells.get(columnIndex)->getColumn()->getIndex() == columnIndex){
                    return m_cells.get(columnIndex);
                }
            }
            for(int i = 0; i < m_cells.size(); i++){
                FCGridCell *cell = m_cells.get(i);
                if(cell->getColumn()->getIndex() == columnIndex){
                    return cell;
                }
            }
        }
        return 0;
    }
    
    FCGridCell* FCGridRow::getCell(const String& columnName){
        for(int i = 0; i < m_cells.size(); i++){
            FCGridCell *cell = m_cells.get(i);
            if(cell->getColumn()->getName() == columnName){
                return cell;
            }
        }
        return 0;
    }
    
    ArrayList<FCGridCell*> FCGridRow::getCells(){
        return m_cells;
    }
    
    void FCGridRow::getAttribute(const String& name, String *value, String *type){
        if (name == L"allowedit"){
            *type = L"bool";
            *value = FCTran::boolToStr(allowEdit());
        }
        else if (name == L"height"){
            *type = L"int";
            *value = FCTran::intToStr(getHeight());
        }
        else if (name == L"visible"){
            *type = L"int";
            *value = FCTran::boolToStr(isVisible());
            
        }
        else{
            *type = L"undefined";
            *value = L"";
        }
    }
    
    ArrayList<String> FCGridRow::getAttributeNames(){
        ArrayList<String> attributeNames;
        attributeNames.add(L"AllowEdit");
        attributeNames.add(L"Height");
        attributeNames.add(L"Visible");
        return attributeNames;
    }
    
    void FCGridRow::onAdd(){
        for(int i = 0; i < m_cells.size(); i++){
            m_cells.get(i)->onAdd();
        }
    }
    
    void FCGridRow::onPaint(FCPaint *paint, const FCRect& clipRect, bool isAlternate){
        bool selected = false;
        FCGridSelectionMode selectionMode = m_grid->getSelectionMode();
        if (selectionMode == FCGridSelectionMode_SelectFullRow){
            ArrayList<FCGridRow*> selectedRows = m_grid->getSelectedRows();
            int selectedRowsSize = (int)selectedRows.size();
            for(int i = 0; i < selectedRowsSize; i++){
                if(selectedRows.get(i) == this){
                    selected = true;
                    break;
                }
            }
        }
        FCGridRowStyle *rowStyle = m_grid->getRowStyle();
        if (isAlternate){
            FCGridRowStyle *alternateRowStyle = m_grid->getAlternateRowStyle();
            if (alternateRowStyle){
                rowStyle = alternateRowStyle;
            }
        }
        if(rowStyle){
            Long backColor = FCColor_None;
            if(selectionMode == FCGridSelectionMode_SelectFullRow){
                if (backColor == FCColor_None){
                    if (selected){
                        backColor = rowStyle->getSelectedBackColor();
                    }
                    else if (this == m_grid->getHoveredRow()){
                        backColor = rowStyle->getHoveredBackColor();
                    }
                    else{
                        backColor = rowStyle->getBackColor();
                    }
                }
            }
            paint->fillRect(backColor, clipRect);
        }
    }
    
    void FCGridRow::onPaintBorder(FCPaint *paint, const FCRect& clipRect, bool isAlternate){
    }
    
    void FCGridRow::onRemove(){
        for(int i = 0; i < m_cells.size(); i++){
            m_cells.get(i)->onRemove();
        }
    }

    void FCGridRow::onScaling(double scaleFactorX, double scaleFactorY) {
        m_height = (int)(int)(m_height * scaleFactorY);
        int cellSize = m_cells.size();
        for (int i = 0; i < cellSize; i++) {
            m_cells.get(i)->onScaling(scaleFactorX, scaleFactorY);
        }
    }
    
    void FCGridRow::removeCell(FCGridColumn *column){
        for(int i = 0; i < m_cells.size(); i++){
            FCGridCell *cell = m_cells.get(i);
            if(cell->getColumn() == column){
                m_cells.removeAt(i);
                cell->onRemove();
                break;
            }
        }
    }
    
    void FCGridRow::removeCell(int columnIndex){
        int cellSize = (int)m_cells.size();
        if(columnIndex >= 0 && columnIndex < cellSize){
            FCGridCell *cell = m_cells.get(columnIndex);
            if(cell->getColumn()->getIndex() == columnIndex){
                m_cells.removeAt(columnIndex);
                return;
            }
            for(int i = 0; i < m_cells.size(); i++){
                cell = m_cells.get(i);
                if(cell->getColumn()->getIndex() == columnIndex){
                    m_cells.removeAt(i);
                    cell->onRemove();
                    break;
                }
            }
        }
    }
    
    void FCGridRow::removeCell(const String& columnName){
        for(int i = 0; i < m_cells.size(); i++){
            FCGridCell *cell = m_cells.get(i);
            if(cell->getColumn()->getName() == columnName){
                m_cells.removeAt(i);
                cell->onRemove();
                break;
            }
        }
    }
    
    void FCGridRow::setAttribute(const String& name, const String& value){
        if (name == L"allowedit"){
            setAllowEdit(FCTran::strToBool(value));
        }
        else if (name == L"height"){
            setHeight(FCTran::strToInt(value));
        }
        else if (name == L"visible"){
            setVisible(FCTran::strToBool(value));
        }
    }
}
